/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import java.util.HashMap;
import java.util.Map;

/**
 * Class, encapsulating request header structure
 * @author Peewhy
 */
public class RequestHeader {
    // Request methods
    public final static int GET = 0;
    public final static int POST = 1;
    public final static String DEFAULT_REQMETHOD = "GET";
    public final static String DEFAULT_HTTPVERS = "HTTP/1.0";
    public final static String DEFAULT_HOST = "localhost";
    public final static String DEFAULT_CONNECTION = "close";
    public final static String DEFAULT_USERAGENT = "Mozilla/4.0";
    public final static String DEFAULT_URL = "/";
    public final static String DEFAULT_LANGUAGE = "en";
    
    
    private String method;
    private String url;
    private String urlQuery;
    private String urlParams;
    
    
    public String httpVersion;
    public String host;
    public String connection;
    public String userAgent;
    public String acceptLanguage;
    public String acceptEncoding;
    public String acceptCharset;
    public String cacheControl;
    

    public String getUrlQuery() {
    	return urlQuery;
    }
    
    public String getUrlParams() {
    	return urlParams;
    }
    
    public String getAcceptCharset() {
        return acceptCharset;
    }

    public void setAcceptCharset(String acceptCharset) {
        this.acceptCharset = acceptCharset;
    }

    public String getAcceptEncoding() {
        return acceptEncoding;
    }

    public void setAcceptEncoding(String acceptEncoding) {
        this.acceptEncoding = acceptEncoding;
    }

    public String getCacheControl() {
        return cacheControl;
    }

    public void setCacheControl(String cacheControl) {
        this.cacheControl = cacheControl;
    }

    public String getConnection() {
        return connection;
    }

    public void setConnection(String connection) {
        this.connection = connection;
    }

    public String getAcceptLanguage() {
        return acceptLanguage;
    }

    public void setAcceptLanguage(String acceptLanguage) {
        this.acceptLanguage = acceptLanguage;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getHttpVersion() {
        return httpVersion;
    }

    public void setHttpVersion(String httpVersion) {
        this.httpVersion = httpVersion;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

	private String getParams(String url) {
		String[] args = url.split("\\?", 2);
		if (args.length > 1) {
			return args[1];
		}
		else {
			return "";
		}
	}
	
	private String getQuery(String url) {
		String[] args = url.split("\\?", 2);
		return args[0];
	}
	
	// Sets URL
    public void setUrl(String url) {
        
        // Extracts URL and PARAMS parts
    	this.url = url;
        this.urlQuery = java.net.URLDecoder.decode(getQuery(this.url));
        this.urlParams = getParams(this.url);
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }
    
    /**
     * Assigns defaults from constants
     */
    public RequestHeader() {
       method =  DEFAULT_REQMETHOD;
       url  = DEFAULT_URL;
       httpVersion = DEFAULT_HTTPVERS;
       host = DEFAULT_HOST;
       connection = DEFAULT_CONNECTION;
       userAgent = DEFAULT_USERAGENT;
       acceptLanguage = DEFAULT_LANGUAGE;       
       }
    
}
